package net.lesscoding.protocol;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LoggingHandler;
import net.lesscoding.common.Const;

import java.nio.charset.StandardCharsets;

/**
 * @author eleven
 * @date 2024/10/30 16:30
 * @apiNote
 */
public class RedisWriteTest {
    public static void main(String[] args) {
        NioEventLoopGroup worker = new NioEventLoopGroup();
        final byte[] LINE = {13, 10};
        try {
            Bootstrap bootstrap = new Bootstrap()
                    .group(worker)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<NioSocketChannel>() {
                        @Override
                        protected void initChannel(NioSocketChannel channel) throws Exception {
                            channel.pipeline()
                                    .addLast(new LoggingHandler())
                                    .addLast(new ChannelInboundHandlerAdapter() {
                                        @Override
                                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                            ByteBuf buffer = ctx.alloc().buffer();
                                            buffer.writeBytes("*3".getBytes())
                                                    .writeBytes(LINE)
                                                    .writeBytes("$3".getBytes())
                                                    .writeBytes(LINE)
                                                    .writeBytes("set".getBytes())
                                                    .writeBytes(LINE)
                                                    .writeBytes("$4".getBytes())
                                                    .writeBytes(LINE)
                                                    .writeBytes("name".getBytes())
                                                    .writeBytes(LINE)
                                                    .writeBytes("$8".getBytes())
                                                    .writeBytes(LINE)
                                                    .writeBytes("zhangsan".getBytes())
                                                    .writeBytes(LINE)
                                            ;
                                            ctx.writeAndFlush(buffer);
                                            super.channelActive(ctx);
                                        }

                                        @Override
                                        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                            ByteBuf byteBuf = (ByteBuf) msg;
                                            System.out.println("接收数据 " + byteBuf.toString(StandardCharsets.UTF_8));
                                            super.channelRead(ctx, msg);
                                        }
                                    })
                            ;
                        }
                    });
            ChannelFuture future = bootstrap.connect(Const.HOST, 6379).sync();
            future.channel().closeFuture().sync();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
